// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MarketFieldCache.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Events;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The market field cache.
    /// </summary>
    public class MarketFieldCache : IMarketFieldCache
    {
        // Where to store Cells
        #region Constants and Fields

        /// <summary>
        /// The marketfields.
        /// </summary>
        private MarketField[] marketfields;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MarketFieldCache"/> class.
        /// </summary>
        public MarketFieldCache()
        {
            this.Initialize();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Related instrument
        /// </summary>
        public IInstrumentStaticData Instrument { get; set; }

        #endregion

        #region Indexers

        /// <summary>
        /// Get or Set a field in cache
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public MarketField this[EMarketField field]
        {
            get
            {
                return this.marketfields[(int)field];
            }

            set
            {
                this.marketfields[(int)field] = value;
            }
        }

        #endregion

        #region Implemented Interfaces

        #region IMarketFieldCache

        /// <summary>
        /// Get a snapshot of current cache
        /// </summary>
        /// <returns>
        /// </returns>
        public FeedEventArgs.MarketFieldUpdate GetSnapshot()
        {
            IList<MarketField> updates = new List<MarketField>();

            foreach (var cell in this.marketfields)
            {
                if (cell != null && cell.HasValue)
                {
                    updates.Add(cell);
                }
            }

            var update = new FeedEventArgs.MarketFieldUpdate(Guid.Empty, updates.ToArray(), true);
            return update;
        }

        /// <summary>
        /// Handle one update
        /// </summary>
        /// <param name="update">
        /// </param>
        public void HandleUpdate(FeedEventArgs.MarketFieldUpdate update)
        {
            // Discard past values if snapshot
            if (update.IsSnapshot)
            {
                this.Initialize();
            }

            foreach (var field in update.Fields)
            {
                this[field.FieldName] = field;
            }
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// The initialize.
        /// </summary>
        private void Initialize()
        {
            var enumValues = Enum.GetValues(typeof(EMarketField));
            this.marketfields = new MarketField[enumValues.Length];
            foreach (var enumValue in enumValues)
            {
                this.marketfields[(int)enumValue] = new MarketField((EMarketField)enumValue);
            }
        }

        #endregion
    }
}